#! /usr/bin/python3

import os
import subprocess

panelAddress = ""
panelTag = ""
srcDir = "/home/lhy/smarthome/edge_rk3308/software/build/edge_midware"
destDir = "/data/changhong/edge_midware"

pushSiteDict = {0:"query_site",
                1:"config_site",
                2:"synergy_site",
                3:"master_coordinate_site",
                4:"ble_site",
                5:"rulesEngine_service_site"}

def printDisplayInfo():
    print("****************")
    for site in pushSiteDict.items():
        print(f"    {site}")
    print("****************")
    

# 站点推送、修改权限、重启
def pushSite(site2push):
    global panelAddress, panelTag
    if(len(panelAddress) == 0):
        panelTag = ""
    else:
        panelTag = f"-s {panelAddress}"
    
    # 推送站点
    os.system(f"adb {panelTag} push {srcDir}/{site2push} {destDir}")
    # 修改站点执行权限
    os.system(f"adb {panelTag} shell chmod 0777 {destDir}/{site2push}")
    # 展示推送站点属性信息
    os.system(f"adb {panelTag} shell ls -l {destDir}/{site2push}")
    # 重启站点
    os.system(f"adb {panelTag} shell killall {site2push}")
   

# 连接ADB到指定的地址
def adbConnect() -> bool:
    global panelAddress
    os.system("adb devices")
    panelAddress = input("please input panel address: ")
    if len(panelAddress) != 0:
        os.system(f"adb connect {panelAddress}")
        code, output = subprocess.getstatusoutput(f"adb -s {panelAddress} shell byjs2023")
    else:
        code, output = subprocess.getstatusoutput(f"adb shell byjs2023")

    if(code != 0):
        print("adb shell failed ...")
        exit
    else:
        return True


if __name__ == "__main__":
    while True:
        adbConnect()
        printDisplayInfo()
        sites2push = input("choose sites to push: ")
        siteIndexs = sites2push.split(' ')
        for siteIndex in siteIndexs:
            pushSite(pushSiteDict[int(siteIndex)])
            
        os.system(f"adb {panelTag} shell sync")


